package com.ydj.app.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.google.gson.reflect.TypeToken;
import com.ydj.app.enumerate.BannerActionEnum;
import com.ydj.app.enumerate.MemberLevelEnum;
import com.ydj.framework.mysql.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.List;

@Getter
@Setter
@Accessors(chain = true)
@Entity
@Table(name = "banner")
@org.hibernate.annotations.Table(appliesTo = "banner", comment = "Banner")
public class Banner extends BaseEntity {
    @Column(columnDefinition = "varchar(50) default '' comment '标题'")
    private String title;

    @Column(columnDefinition = "varchar(300) comment '图片'", nullable = false)
    private String image;

    @Column(columnDefinition = "int default 0 comment '排序号'")
    private Integer orderNumber;

    @Column(columnDefinition = "tinyint comment '动作'", nullable = false)
    private BannerActionEnum action;

    @Column(columnDefinition = "varchar(300) comment '跳转的URL（如果动作是跳转）'")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String actionUrl;

    @Column(columnDefinition = "json comment '会员等级'", nullable = false)
    private String memberLevelJson;

    @Column(columnDefinition = "tinyint default 0 comment '是否上架'", nullable = false)
    private Boolean enable;

    @Column(columnDefinition = "varchar(500) default '' comment '备注'")
    private String remark;

    public List<MemberLevelEnum> getMemberLevelList() {
        return gson.fromJson(memberLevelJson, new TypeToken<List<MemberLevelEnum>>() {}.getType());
    }

    public Banner setMemberLevelList(List<MemberLevelEnum> value) {
        memberLevelJson = gson.toJson(value);
        return this;
    }
}